package com.sdry.web.controller.zc.quality;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.llm.User;
import com.sdry.model.zc.ZcApproverResultEntity;
import com.sdry.model.zc.ZcDisposeRejectsEntity;
import com.sdry.model.zc.ZcDisposeRejectsEntity2Android;
import com.sdry.model.zc.ZcProductTestEntity;
import com.sdry.model.zc.ZcQueryCriteria;
import com.sdry.service.zc.ZcDisposeRejectsApprovalService;
import com.sdry.utils.PushExample;
import com.sdry.utils.ResponseUtil;

/**
 * 
 * @ClassName:    ZcDisposeRejectsApprovalController   
 * @Description:  不合格处置结果审批
 * @Author:       zc   
 * @CreateDate:   2018年11月1日 上午10:52:42   
 * @Version:      v1.0
 */
@Controller
@RequestMapping("disposeRejectsApproval")
public class ZcDisposeRejectsApprovalController {

	@Resource
	private ZcDisposeRejectsApprovalService zcDisposeRejectsApprovalService;
	/**
	 * 进入不合格处置结果审批页面
	 * @return
	 */
	@RequestMapping("enterDisposeRejectsApprovalPage")
	public String enterDisposeRejectsApprovalPage(){
		return "/zc/quality/disposeRejectsApproval";
	}
	/**
	 * 进入我发起的审批页面
	 * @return
	 */
	@RequestMapping("enterMyApprovelPage")
	public String enterMyApprovelPage(){
		return "/zc/quality/myApprovel";
	}
	/**
	 * 进入我已审批的处置单页面
	 * @return
	 */
	@RequestMapping("enterMyAlreadyApprovelPage")
	public String enterMyAlreadyApprovelPage(){
		return "/zc/quality/myAlreadyApprovel";
	}
	/**
	 * 进入抄送我的处置单页面
	 * @return
	 */
	@RequestMapping("enterCopyMeApprovelPage")
	public String enterCopyMeApprovelPage(){
		return "/zc/quality/copyMeApprovel";
	}
	/**
	 * 查找不合格处置待审批列表
	 * @param request
	 * @param response
	 */
	@RequestMapping("selectDisposeRejectsList4Me")
	public void selectDisposeRejectsList4Me(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria,HttpSession httpSession){
		if(zcQueryCriteria.getKeyword() == null){
			zcQueryCriteria.setKeyword("");
		}
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcQueryCriteria.setWorker(userName);
		List<ZcDisposeRejectsEntity> disposeRejectsList = zcDisposeRejectsApprovalService.selectDisposeRejectsList4Me(zcQueryCriteria);
		int count = zcDisposeRejectsApprovalService.countDisposeRejectsList4Me(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(disposeRejectsList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 提交审批
	 * @param request
	 * @param response
	 * @param zcApproverResultEntity
	 */
	@RequestMapping("submitApprover")
	public void submitApprover(HttpServletRequest request,HttpServletResponse response,ZcApproverResultEntity zcApproverResultEntity,HttpSession httpSession){
		Date date = new Date();
		zcApproverResultEntity.setDate(date);
		int affact = zcDisposeRejectsApprovalService.submitApprover(zcApproverResultEntity);
		ZcDisposeRejectsEntity zcDisposeRejectsEntity = zcDisposeRejectsApprovalService.selectZcDisposeRejectsEntity(zcApproverResultEntity.getdId()+"");
		String s = "";
		if("2".equals(zcDisposeRejectsEntity.getdApproverStatus())){
			s = zcDisposeRejectsEntity.getdApprover2();
		}else if("3".equals(zcDisposeRejectsEntity.getdApproverStatus())){
			s = zcDisposeRejectsEntity.getdApprover3();
		}
		int testSendPush = PushExample.testSendPush("您有一个新的不合格处置单需要审批", "您有一个新的不合格处置单需要审批", s, "1");
		System.err.println(testSendPush);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查询我发起的审批
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectMyApprovelList")
	public void selectMyApprovelList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcQueryCriteria.setWorker(userName);
		if(zcQueryCriteria.getKeyword() == null){
			zcQueryCriteria.setKeyword("");
		}
		List<ZcDisposeRejectsEntity> myApprovelList = zcDisposeRejectsApprovalService.selectMyApprovelList(zcQueryCriteria);
		int count = zcDisposeRejectsApprovalService.countMyApprovelList(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(myApprovelList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查找我已审批的处置单
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 * @param httpSession
	 */
	@RequestMapping("selectMyAlreadyApprovelList")
	public void selectMyAlreadyApprovelList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcQueryCriteria.setWorker(userName);
		if(zcQueryCriteria.getKeyword() == null){
			zcQueryCriteria.setKeyword("");
		}
		List<ZcDisposeRejectsEntity> myAlreadyApprovelList = zcDisposeRejectsApprovalService.selectMyAlreadyApprovelList(zcQueryCriteria);
		int count = zcDisposeRejectsApprovalService.countMyAlreadyApprovelList(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(myAlreadyApprovelList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 查找抄送我的处置单
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 * @param httpSession
	 */
	@RequestMapping("selectCopyMeApprovelList")
	public void selectCopyMeApprovelList(HttpServletRequest request,HttpServletResponse response,ZcQueryCriteria zcQueryCriteria,HttpSession httpSession){
		User user = (User) httpSession.getAttribute("currentUser");
		String userName = user.getUserName();
		zcQueryCriteria.setWorker(userName);
		if(zcQueryCriteria.getKeyword() == null){
			zcQueryCriteria.setKeyword("");
		}
		List<ZcDisposeRejectsEntity> myAlreadyApprovelList = zcDisposeRejectsApprovalService.selectCopyMeApprovelList(zcQueryCriteria);
		int count = zcDisposeRejectsApprovalService.countCopyMeApprovelList(zcQueryCriteria);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(myAlreadyApprovelList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/************************************************************************************************************
	 * 为安卓查询待审批信息	
	 * @param request
	 * @param response
	 */
	@RequestMapping("selectDisposeRejectsList2Android")
	public void selectDisposeRejectsList2Android(HttpServletRequest request,HttpServletResponse response,String worker){
		List<ZcDisposeRejectsEntity> disposeRejectsList = zcDisposeRejectsApprovalService.selectDisposeRejectsList2Android(worker);
		List<ZcDisposeRejectsEntity2Android> disposeRejectsListandroid=new ArrayList<>();
		ZcDisposeRejectsEntity2Android disposeRejectsList2android=new ZcDisposeRejectsEntity2Android();
		for(int i=0;i<disposeRejectsList.size();i++) {
			disposeRejectsList2android = new ZcDisposeRejectsEntity2Android();
			disposeRejectsList2android.getClass(disposeRejectsList.get(i));
			//disposeRejectsListandroid.set(i, disposeRejectsList2android);
			disposeRejectsListandroid.add(disposeRejectsList2android);
		}
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(disposeRejectsListandroid);
		try {
			if(disposeRejectsListandroid.size()>0){
				jsonObject.put("state", 1);
				jsonObject.put("message", "查找成功");
				jsonObject.put("data", jsonArray);
			}else{
				jsonObject.put("state", 0);
				jsonObject.put("message", "查找失败");
				jsonObject.put("data", null);
			}
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 提交审批android
	 * @param request
	 * @param response
	 * @param zcApproverResultEntity
	 */
	@RequestMapping("androidsubmitApprover")
	public void androidsubmitApprover(HttpServletRequest request,HttpServletResponse response,String dId,String opinion){
		ZcApproverResultEntity zcApproverResultEntity = new ZcApproverResultEntity();
		Date date = new Date();
		zcApproverResultEntity.setDate(date);
		zcApproverResultEntity.setdId(Long.parseLong(dId));
		zcApproverResultEntity.setOpinion(opinion);
		int affact = zcDisposeRejectsApprovalService.submitApprover(zcApproverResultEntity);
		ZcDisposeRejectsEntity zcDisposeRejectsEntity = zcDisposeRejectsApprovalService.selectZcDisposeRejectsEntity(dId);
		String s = "";
		if("2".equals(zcDisposeRejectsEntity.getdApproverStatus())){
			s = zcDisposeRejectsEntity.getdApprover2();
		}else if("3".equals(zcDisposeRejectsEntity.getdApproverStatus())){
			s = zcDisposeRejectsEntity.getdApprover3();
		}
		int testSendPush = PushExample.testSendPush("您有一个新的不合格处置单需要审批", "您有一个新的不合格处置单需要审批", s, "1");
		System.err.println(testSendPush);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("state", affact);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 安卓查询我发起的审批
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 */
	@RequestMapping("selectMyApprovelListAndroid")
	public void selectMyApprovelListAndroid(HttpServletRequest request,HttpServletResponse response,String worker){
		
		List<ZcDisposeRejectsEntity> disposeRejectsList = zcDisposeRejectsApprovalService.selectMyApprovelList2Android(worker);
		

		List<ZcDisposeRejectsEntity2Android>  disposeRejectsListandroid=new ArrayList<>();
		ZcDisposeRejectsEntity2Android disposeRejectsList2android=new ZcDisposeRejectsEntity2Android();
		for(int i=0;i<disposeRejectsList.size();i++) {
			disposeRejectsList2android = new ZcDisposeRejectsEntity2Android();
			disposeRejectsList2android.getClass(disposeRejectsList.get(i));
			disposeRejectsListandroid.add(disposeRejectsList2android);
		}
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(disposeRejectsListandroid);
		try {
			if(disposeRejectsListandroid.size()>0){
				jsonObject.put("state", 1);
				jsonObject.put("message", "查找成功");
				jsonObject.put("data", jsonArray);
			}else{
				jsonObject.put("state", 0);
				jsonObject.put("message", "查找失败");
				jsonObject.put("data", null);
			}
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 安卓查找我已审批的处置单
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 * @param httpSession
	 */
	@RequestMapping("selectMyAlreadyApprovelListAndroid")
	public void selectMyAlreadyApprovelListAndroid(HttpServletRequest request,HttpServletResponse response,String worker){
		
		List<ZcDisposeRejectsEntity> disposeRejectsList = zcDisposeRejectsApprovalService.selectMyAlreadyApprovelList2Android(worker);
		
		List<ZcDisposeRejectsEntity2Android>  disposeRejectsListandroid=new ArrayList<>();
		ZcDisposeRejectsEntity2Android disposeRejectsList2android=new ZcDisposeRejectsEntity2Android();
		for(int i=0;i<disposeRejectsList.size();i++) {
			disposeRejectsList2android = new ZcDisposeRejectsEntity2Android();
			disposeRejectsList2android.getClass(disposeRejectsList.get(i));
			disposeRejectsListandroid.add(disposeRejectsList2android);
		}
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(disposeRejectsListandroid);
		try {
			if(disposeRejectsListandroid.size()>0){
				jsonObject.put("state", 1);
				jsonObject.put("message", "查找成功");
				jsonObject.put("data", jsonArray);
			}else{
				jsonObject.put("state", 0);
				jsonObject.put("message", "查找失败");
				jsonObject.put("data", null);
			}
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 安卓查找抄送我的处置单
	 * @param request
	 * @param response
	 * @param zcQueryCriteria
	 * @param httpSession
	 */
	@RequestMapping("selectCopyMeApprovelListAndroid")
	public void selectCopyMeApprovelListAndroid(HttpServletRequest request,HttpServletResponse response,String worker){
		List<ZcDisposeRejectsEntity> disposeRejectsList = zcDisposeRejectsApprovalService.selectCopyMeApprovelList2Android(worker);
		List<ZcDisposeRejectsEntity2Android>  disposeRejectsListandroid=new ArrayList<>();
		ZcDisposeRejectsEntity2Android disposeRejectsList2android=new ZcDisposeRejectsEntity2Android();
		for(int i=0;i<disposeRejectsList.size();i++) {
			disposeRejectsList2android = new ZcDisposeRejectsEntity2Android();
			disposeRejectsList2android.getClass(disposeRejectsList.get(i));
			disposeRejectsListandroid.add(disposeRejectsList2android);
		}
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = JSONArray.fromObject(disposeRejectsListandroid);
		try {
			if(disposeRejectsListandroid.size()>0){
				jsonObject.put("state", 1);
				jsonObject.put("message", "查找成功");
				jsonObject.put("data", jsonArray);
			}else{
				jsonObject.put("state", 0);
				jsonObject.put("message", "查找失败");
				jsonObject.put("data", null);
			}
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}	
	/**
	 * 通过条码查询产品检验单
	 * @param request
	 * @param response
	 * @param code
	 */
	@RequestMapping("selectProductTestByCode")
	public void selectProductTestByCode(HttpServletRequest request,HttpServletResponse response,String code){
		if(code==null){
			code = "";
		}
		ZcProductTestEntity zcProductTestEntity = zcDisposeRejectsApprovalService.selectProductTestByCode(code);
		JSONObject json= JSONObject.fromObject(zcProductTestEntity);  
		try {
			if(zcProductTestEntity!=null){
				ResponseUtil.write(response, json);
			}else{
				ResponseUtil.write(response, 0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
